﻿using System;
using System.Collections.Generic;
using System.Text;
using ECC_EXT_Entity;
using NHibernate;
using NHibernate.Criterion;

namespace ECC_EXT_DAO.Dao
{
    public class OFFAnnotationDao : DaoBase
    {

        public void SaveOrUpdate(OFFAnnotationEntity _offAnnotationEntity)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                    session.BeginTransaction();
                    session.SaveOrUpdate(_offAnnotationEntity);
                    session.Transaction.Commit();
            }
        }

        public OFFAnnotationEntity GetById(OFFAnnotationEntity _offAnnotationEntity)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                return
                    session.Get<OFFAnnotationEntity>(_offAnnotationEntity.ID) as OFFAnnotationEntity;
            }
        }

        public IList<OFFAnnotationEntity> Search(OFFAnnotationEntitySearch _offAnnotationEntitySearch)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                ICriteria criteria =
                    session.CreateCriteria<OFFAnnotationEntity>();
                criteria.Add(Restrictions.Between("RecordDate",
                                   _offAnnotationEntitySearch.RecordDateFrom,
                                   _offAnnotationEntitySearch.RecordDateTo.AddDays(1)));

                if (!"".Equals(_offAnnotationEntitySearch.RecordUser))
                    criteria.Add(Restrictions.Like("RecordUser",
                                                 _offAnnotationEntitySearch.RecordUser,
                                                 MatchMode.Anywhere));

                criteria.Add(Restrictions.Eq("dicOFFAnnotatonStatus",
                                                 _offAnnotationEntitySearch.Status));
                
                return criteria.List<OFFAnnotationEntity>();
            }
        }
    }
}
